package com.bjfu.gmms.servlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.StudentInfoMapper;
import com.bjfu.gmms.pojo.StudentInfo;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;

@WebServlet("/studentInfoSubmit")
public class StudentInfoSubmitServlet extends HttpServlet {

    private StudentInfoMapper studentInfoMapper;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 使用 try-with-resources 来确保 SqlSession 被自动关闭
        request.setCharacterEncoding("UTF-8");
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession("development")) {
            studentInfoMapper = sqlSession.getMapper(StudentInfoMapper.class);
            // 获取前端传来的数据
            int studentId = Integer.parseInt(request.getParameter("studentId"));
            String name = request.getParameter("name");
            String gender = request.getParameter("gender");
            int category1 = Integer.parseInt(request.getParameter("category1"));
            int category2 = Integer.parseInt(request.getParameter("category2"));
            String undergraduateSchool = request.getParameter("undergraduateSchool");
            Date graduationDate = Date.valueOf(request.getParameter("graduationDate"));
            String undergraduateMajor = request.getParameter("undergraduateMajor");
            String phone = request.getParameter("phone");
            String emergencyPhone = request.getParameter("emergencyPhone");
            Date birthDate = Date.valueOf(request.getParameter("birthDate"));
            String idNumber = request.getParameter("idNumber");
            String sourceLocation = request.getParameter("sourceLocation");
            String mail = request.getParameter("mail");
            String undergraduateSchoolType = request.getParameter("undergraduateSchoolType");
            String personalResume = request.getParameter("personalResume");
            String originalAppliedDiscipline = request.getParameter("originalAppliedDiscipline");

            // 改：根据学科名称查询学科 ID（字符串类型）
            String disciplineId = studentInfoMapper.getDisciplineIdByName(originalAppliedDiscipline);
            if (disciplineId == null) {
                throw new ServletException("无法找到对应的学科ID");
            }

            // 创建 StudentInfo 对象
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setStudentId(studentId);
            studentInfo.setName(name);
            studentInfo.setGender(gender);
            studentInfo.setCategory1(category1);
            studentInfo.setCategory2(category2);
            studentInfo.setUndergraduateSchool(undergraduateSchool);
            studentInfo.setGraduationDate(graduationDate);
            studentInfo.setUndergraduateMajor(undergraduateMajor);
            studentInfo.setPhone(phone);
            studentInfo.setEmergencyPhone(emergencyPhone);
            studentInfo.setBirthDate(birthDate);
            studentInfo.setIdNumber(idNumber);
            studentInfo.setSourceLocation(sourceLocation);
            studentInfo.setMail(mail);
            studentInfo.setUndergraduateSchoolType(undergraduateSchoolType);
            studentInfo.setPersonalResume(personalResume);
            studentInfo.setOriginalAppliedDiscipline(disciplineId);

            // 插入学生信息
            studentInfoMapper.insertStudentInfo(studentInfo);

            // 提交事务
            sqlSession.commit();

            // 跳转到调剂意愿页面
            request.setAttribute("studentId", studentId);
            request.getRequestDispatcher("adjustment.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException("Failed to submit student information", e);
        }
    }
}

